package com.example.java_chatroom.api;

import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.FriendMapper;
import com.example.java_chatroom.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Q
 * @create 2025-07-06-11:50
 */
@RestController
public class FriendAPI {
    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req) {
        //1.从会话中，获取userId
        HttpSession session = req.getSession(false);
        if(session == null) {
            System.out.println("[getFriendList]获取不到session对象");
            return new ArrayList<Friend>();//返回空user
        }
        //2.从会话中获取到用户对象信息
        User user = (User) session.getAttribute("user");
        if(user == null) {
            System.out.println("[getFriendList]获取不到user对象");
            return new ArrayList<Friend>();//返回空user
        }
        //2.根据userId，查询数据库数据
        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;
    }
}
